package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.constant.UserConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.website.domain.WebsiteSearchLog;
import com.cz.video.website.domain.WebsiteSearchLog;
import com.cz.video.website.domain.WebsiteSearchLog;
import com.cz.video.website.domain.bo.WebsiteSearchLogBo;
import com.cz.video.website.domain.bo.WebsiteSearchLogBo;
import com.cz.video.website.domain.vo.WebsiteSearchLogVo;
import com.cz.video.website.mapper.WebsiteSearchLogMapper;
import com.cz.video.website.mapper.WebsiteSearchLogMapper;
import com.cz.video.website.service.IWebsiteSearchLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 搜索日志表 服务实现类
 * </p>
 *
 * @author LLF
 * @since 2023-10-16
 */
@Service
@RequiredArgsConstructor
public class WebsiteSearchLogServiceImpl extends ServiceImpl<WebsiteSearchLogMapper, WebsiteSearchLog> implements IWebsiteSearchLogService {
    private final WebsiteSearchLogMapper websiteSearchLogMapper;

    /**
     * 新增保存搜索日志表信息
     *
     * @param websiteSearchLog 用户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSearchLog(WebsiteSearchLog websiteSearchLog) {
        int rows = websiteSearchLogMapper.insert(websiteSearchLog);
        return rows;
    }

    /**
     * 修改搜索日志表信息
     * @param websiteSearchLog
     * @return
     */
    @Override
    public int updateSearchLog(WebsiteSearchLogBo websiteSearchLog) {
        return websiteSearchLogMapper.updateById(websiteSearchLog);
    }

    /**
     * 分页查找搜索日志表信息
     *
     * @param websiteSearchLog
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<WebsiteSearchLogVo> selectPageSearchLogList(WebsiteSearchLogBo websiteSearchLog, PageQuery pageQuery) {
        Page<WebsiteSearchLogVo> page=websiteSearchLogMapper.selectPageSearchLogList(pageQuery.build(),this.buildQueryWrapper(websiteSearchLog));
        return TableDataInfo.build(page);
    }

    @Override
    public int deleteById(int id) {
        return websiteSearchLogMapper.deleteById(id);
    }

    /**
     * 根据查询条件构建query wrapper
     *
     * @param websiteSearchLog 查询条件
     *
     * @return query wrapper
     */
    private Wrapper<WebsiteSearchLog> buildQueryWrapper(WebsiteSearchLogBo websiteSearchLog) {
        Map<String, Object> params = websiteSearchLog.getParams();
        QueryWrapper<WebsiteSearchLog> wrapper = Wrappers.query();

        wrapper.eq("sl.del_flag", UserConstants.USER_NORMAL)
            .eq(ObjectUtil.isNotNull(websiteSearchLog.getSearchLogId()), "sl.SearchLog_id", websiteSearchLog.getSearchLogId())
            .eq(ObjectUtil.isNotNull(websiteSearchLog.getAccountId()), "sl.Account_id", websiteSearchLog.getAccountId())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "sl.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
